/*
Chris Elrod
10/27/2008
CSci 252
Prof. Hasan

Project 2 - EJB

Reference: 
    BEA trader example: BEA 10.3 HOME\wlserver_10.3\samples\server\examples\src\examples\ejb\ejb20\basic\statefulSession
*/

package examples.ejb.ejb20.basic.Project2;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.ejbgen.*;
import java.util.Hashtable;

/**
 * ProfileBean is a stateless Session Bean. This bean illustrates:
 * 
 * Automatic persistence of state between calls to the Session Bean
 * Looking up values from the Environment
 * 
 */

@FileGeneration(remoteClass = Constants.Bool.TRUE,
                localHome = Constants.Bool.FALSE,
                remoteHome = Constants.Bool.TRUE,
                remoteClassName = "Profile",
                remoteHomeName = "ProfileHome",
                localClass = Constants.Bool.FALSE)
@JarSettings(ejbClientJar = "Project2_client.jar")
@JndiName(remote = "Project2-Profile-ProfileHome")
@Session(idleTimeoutSeconds = "600",
         maxBeansInCache = "1000",
         transTimeoutSeconds = "0",
         type = Session.SessionType.STATEFUL,
         defaultTransaction = Constants.TransactionAttribute.REQUIRED,
         ejbName = "statelessSession_Profile",
         enableCallByReference = Constants.Bool.TRUE)


public class ProfileBean implements SessionBean {


  private SessionContext ctx;
  private Hashtable table = new Hashtable();


  public ProfileBean(){
    this.table.put( new String("Omair"), new String("1277 Seventh St. Boise ID 56789") );
    this.table.put( new String("John34"), new String("522 Red St. Arlington VA 23233") );
  }


  private void log(String s) {
    System.out.println(s);
  } 

  
  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbActivate() {
    log("ejbActivate called");
  }


  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbRemove() {
    log("ejbRemove called");
  }


  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbPassivate() {
    log("ejbPassivate called");
  }


  /**
   * 
   * This method is required by the EJB Specification,
   * but is not used by this example.
   *
   * Sets the session context.
   *
   * @param ctx               SessionContext Context for session
   */
  public void setSessionContext(SessionContext ctx) {
    log("setSessionContext called");
    this.ctx = ctx;
  }


  /**
   * 
   *
   * This method corresponds to the create method in the home interface
   * "ProfileHome.java".
   * The parameter sets of the two methods are identical. When the client calls
   * <code>ProfileHome.create()</code>, the container allocates an instance of 
   * the EJBean and calls <code>ejbCreate()</code>.
   *
   * @exception               javax.ejb.CreateException if there is
   *                          a communications or systems failure
   * @see                     examples.ejb.ejb20.basic.statelessSession.Profile
   */

  public void ejbCreate () throws CreateException {
    log("ejbCreate called");

    try {
      InitialContext ic = new InitialContext();
    } catch (NamingException ne) {
      throw new CreateException("Failed to find environment value "+ne);
    }
  }


  /*Returns an address based on a match between the argument uid and the ProfileBean's hastable.
    Parms: user id.  Returns the address of the user id.*/ 
  @RemoteMethod()
  
  public String getAddress( String uid ){   
    return table.get(uid).toString();
  }


  /*Adds a profile to the hashtable 
    Parms: user id and address.  Returns a boolean based on the success of the method.*/
  @RemoteMethod()
  
  public boolean addProfile(String uid, String address){   
    try{
        table.put(uid,address);     
    }catch(Exception ae){ 
      log("An Exception has been thrown in the AddressReader constructor: " + ae);  
      return false;
    } 

    return true;    
  }

}








